package com.example.myblog01.handler;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

//@ControllerAdvice 会拦截所有标注Controller的注解  .
@ControllerAdvice
public class ControllerExceptionHandler {


    //拦截异常信息 从控制台输出

    private final Logger logger = LoggerFactory. getLogger(this.getClass());
    @ExceptionHandler(Exception.class)
    public ModelAndView exceptionHander(HttpServletRequest request, Exception e) {
        logger.error("Requst URL : "+request.getRequestURL()+",Exception : {}", e);
        ModelAndView mv = new ModelAndView();
        mv.addObject("url", request.getRequestURL());
        mv.addObject("exception", e);
        mv.setViewName("error/error");
        return mv;

    }

}
